package com.hgts.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgts.common.R;
import com.hgts.pojo.Book;
import com.hgts.pojo.Employee;
import com.hgts.pojo.Role;
import com.hgts.pojo.dto.EmployeeDto;
import com.hgts.service.EmployeeService;
import com.hgts.service.IRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private IRoleService roleService;

    /**
     * 员工登录
     * @param
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<EmployeeDto> login(@RequestBody Employee employee){

        return employeeService.getEmployee(employee);
    }

    /**
     * 退出登录
     * @param request
     * @return
     */
    /*@PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        //清理Session中当前保存的员工id
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }*/

    /**
     * 获取用户信息
     * @return
     */
    @GetMapping("info")
    public R info(String token) {
        Map<String, Object> map = new HashMap<>();
        map.put("roles","[admin]");
        map.put("name","admin");
        map.put("avatar","https://oss.aliyuncs.com/aliyun_id_photo_bucket/default_handsome.jpg");
        return R.success(map);
    }
    /**
     * 退出
     * @return
     */
    @PostMapping("logout")
    public R logout(){
        return R.success("");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {


        return employeeService.pageInfo(page, pageSize, name);
    }

    /**
     * 新增员工
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee) {
        //设置初始密码123456，需要进行md5加密处理
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        employeeService.addEmployee(employee);

        return R.success("新增员工成功");
    }

    /**
     * 编辑功能
     * @param request
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request, @RequestBody Employee employee){
        log.info(employee.toString());

        //Long empId = (Long) request.getSession().getAttribute("employee");
        //employee.setUpdateTime(LocalDateTime.now());
        //employee.setUpdateUser(empId);
        //获取角色id
        Integer roleId = employee.getRoleId();
        //根据角色id更改角色信息
        Role role = roleService.getById(roleId);
        if (role != null) {
            String roleRole = role.getRole();
            employee.setRole(roleRole);
            employeeService.updateById(employee);
            return R.success("员工信息修改成功");
        }
        int status = employee.getStatus();
        employee.setStatus(status);
        employeeService.updateById(employee);
        return R.success("员工状态修改成功");
    }
    @GetMapping("/status")
    public R<String> status( int id, int status){
        LambdaUpdateWrapper<Employee> updateWrapper = new LambdaUpdateWrapper<>();

        updateWrapper.eq(Employee::getId,id);
        updateWrapper.set(Employee::getStatus,status);
        employeeService.update(updateWrapper);
        return R.success("员工信息修改成功");
    }


    /**
     * 获取图书信息进行回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getEmployeeById(@PathVariable int id){
        return employeeService.getEmployeeRole(id);
    }

    /*@GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){

        Employee employee = employeeService.getById(id);
        if (employee != null){
            return R.success(employee);
        }
        return R.error("没有查询到对应的员工信息");
    }*/
}
